/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lucene.Analyzers;

import java.io.IOException;
import java.util.Stack;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;

public class SynonymFilterIgnoreHashtag extends TokenFilter {
    
    public static final String TOKEN_TYPE_SYNONYM = "SYNONYM";
    private Stack synonymStack;
    private SynonymEngine engine;
    
    public SynonymFilterIgnoreHashtag(TokenStream in, SynonymEngine engine) {
        super(in);
        synonymStack = new Stack();
        this.engine = engine;
    }
    
    public Token next() throws IOException {
        if (synonymStack.size() > 0) {
            return (Token) synonymStack.pop();
        }        
        Token token = input.next();
        if (token == null) {
            return null;
        }
        addAliasesToStack(token);
        return token;
    }
    
    private void addAliasesToStack(Token token) throws IOException {
        if (token.termText().startsWith("#"))
            return;
        String[] synonyms = engine.getSynonyms(token.termText());
        if (synonyms == null) return;
        for (int i = 0; i < synonyms.length; i++) {
            Token synToken = new Token(synonyms[i], 
                    token.startOffset(),token.endOffset(),TOKEN_TYPE_SYNONYM);
            synToken.setPositionIncrement(0);
            synonymStack.push(synToken);
        }
    }
}